package com.example.demo.repository;

import com.example.demo.entity.Buyer.BuyPlanBill;
import com.example.demo.entity.Buyer.BuyPlanBillDetail;
import jakarta.transaction.Transactional;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

@Repository
public interface BuyPlanBillRepository extends CrudRepository<BuyPlanBill, Integer> {

    @Query(value = "SELECT v FROM BuyPlanBill v WHERE v.billNo LIKE CONCAT('%', :billNo, '%')")
    List<BuyPlanBill> findByBillNo(String billNo);

    @Query("select b from BuyPlanBillDetail b where b.billNo = :billNo")
    List<BuyPlanBillDetail> findDetail(String billNo);

    @Query("select b.buyerCode,b.buyerName from Buyer b where b.buyerStatus = 'y' ")
    List<Object[]> getBuyer();

    @Query("select b.billNo from BuyPlanBillDetail b where b.billNo = :billNo")
    String getBillNo(String billNo);

    @Modifying
    @Transactional
    @Query("update BuyPlanBillDetail b set b.planDate = :planDate, b.buyerCode = :buyerCode, b.remark = :remark where b.billNo = :billNo")
    void setForm(String billNo, Date planDate, String buyerCode, String remark);

    @Modifying
    @Transactional
    @Query(value = "insert into buy_plan_master(bill_no, plan_date, buyer_code, remark) values(:billNo, :planDate, :buyerCode, :remark)", nativeQuery = true)
    void addForm(String billNo, Date planDate, String buyerCode, String remark);
}
